/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CardGame;

import DataManager.DbManager;
import DataManager.MySQLCommonDataProvider;
import Service.CardConstant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Apolloz
 */
public class RoomManager {

    public static Logger log = LoggerFactory.getLogger(RoomManager.class.getName());
    // room name - list of table
    private static Map<String, ArrayList> rooms = Collections.synchronizedMap(new HashMap<String, ArrayList>());
    // table name - GameTable (userlist)
    private static Map<String, GameTable> tables = Collections.synchronizedMap(new HashMap<String, GameTable>());
    private static Map<String, GameRoom> roomProperties = Collections.synchronizedMap(new LinkedHashMap<String, GameRoom>());
    private static ArrayList<String[]> tableRandomList = new ArrayList<String[]>();
    private static RoomManager roomManager = null;

    public synchronized static RoomManager getInstance() {
        if (roomManager == null) {
            roomManager = new RoomManager();
        }
        return roomManager;
    }

    public RoomManager() {
        initGameTables();
    }

    public GameTable getTable(String tableID) {
        return RoomManager.tables.get(tableID);
    }

    public ArrayList getTableList(String roomID) {
//        shuffleTables(roomID);
        return rooms.get(roomID);
    }
//    public String[] getTableList() {
//        Random rdm = new Random();
//        int randomNumber = rdm.nextInt(99);
//        return (String[]) tableRandomList.get(randomNumber);
//    }

    public GameRoom getRoomProperties(String roomID) {
        return roomProperties.get(roomID);
    }

    public Collection getRoomList() {
        return (Collection) roomProperties.keySet();
    }

    public boolean isTableExist(String tableID) {
        return tables.containsKey(tableID);
    }

    private void initGameTables() {
        MySQLCommonDataProvider dataManager = DbManager.GetCommonDataManager();
        ArrayList roomList = new ArrayList();// = dataManager.getRoomList(CardConstant.TIENLEN_TYPE);
        GameRoom g1 = new GameRoom();
        g1.code = "TL0001";
        g1.type = 0;
        g1.money = 10000;
        g1.noOfTables = 30;
        roomList.add(g1);

        g1 = new GameRoom();
        g1.code = "TL0002";
        g1.type = 1;
        g1.money = 20000;
        g1.noOfTables = 30;
        roomList.add(g1);

        g1 = new GameRoom();
        g1.code = "TL0003";
        g1.type = 2;
        g1.money = 30000;
        g1.noOfTables = 30;
        roomList.add(g1);

        int noOfRooms = roomList.size();
        String[] randomTables = new String[PIServer.VMSProtocolHandler.MAX_GAME_TABLE_PER_ROOM];
        for (int i = 0; i < noOfRooms; i++) {
            //ArrayList r = (ArrayList)arrTLRoomList.get(i);
            // order: id, code, name, type, bet money
//            int type= (Integer)r.get(3);
//            int bet = (Integer)r.get(4);
//            String roomCode = (String)r.get(1);
            GameRoom gr = (GameRoom) roomList.get(i);
            ArrayList arrTableList = new ArrayList(gr.noOfTables);
            //String roomName = String.format("TL%04d", i);
            //GameRoomManager.rooms.put("TL00"+i,temp);
            rooms.put(gr.code, arrTableList);
            roomProperties.put(gr.code, gr);
            for (int j = 0; j < gr.noOfTables; j++) {
                GameTable gt = new GameTable();
                String tableName = String.format("%02d", j);
                gt.tableID = gr.code + tableName;
                gt.setMaxPlayers(4);
                gt.tableStatus = 0;
                gt.betMoney = (j + 1) * gr.money;
                gt.betMoneyDefault = (j + 1) * gr.money;
                gt.tableType = gr.type;
                RoomManager.tables.put(gt.tableID, gt);
                arrTableList.add(gt);
                randomTables[j] = tableName;
            }
        }

        for (int i = 0; i < 100; i++) {
            //.out.println("  : " + i);
            String[] newRandomList = shuffleArray(randomTables);
            tableRandomList.add(i, newRandomList);
            //System.out.println("_________________");
        }
        log.info("Room numbers=" + RoomManager.rooms.size());
    }

    public String[] shuffleArray(String[] arr) {
        Random rdm = new Random();
        int length = arr.length;
        String[] result = new String[length];
        for (int i = 0; i < length; i++) {
            result[i] = arr[i];
        }
        for (int i = 0; i < length; i++) {
            int posA = rdm.nextInt(length - 1);
            String tmp = result[posA];
            result[posA] = result[i];
            result[i] = tmp;
        }
//        for (int i = 0; i < length; i++) {
//            System.out.println(result[i] + "  ");
//        }
        return result;
    }

    public void shuffleTables(String roomID) {
        ArrayList roomTables = rooms.get(roomID);
        synchronized (roomTables) {
            Collections.shuffle(roomTables);
        }
    }
}
